﻿var ecollabroApp = angular.module('ecollabroApp', []);
ecollabroApp.config(['$httpProvider', function ($httpProvider) {
    $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';
}]);
ecollabroApp.config(['$httpProvider',function ($httpProvider) {
    $httpProvider.responseInterceptors.push('myHttpInterceptor');
    var spinnerFunction = function (data, headersGetter) {
        // todo start the spinner here
        $('#loading').show();
        return data;
    };
    $httpProvider.defaults.transformRequest.push(spinnerFunction);
}])
// register the interceptor as a service, intercepts ALL angular ajax http calls
ecollabroApp.factory('myHttpInterceptor', ['$q', '$window', function ($q, $window) {
    return function (promise) {
        return promise.then(function (response) {
            // do something on success
            // todo hide the spinner
            $('#loading').hide();
            return response;

        }, function (response) {
            // do something on error
            // todo hide the spinner
            $('#loading').hide();
            return $q.reject(response);
        });
    };
}])
ecollabroApp.factory('$eCollabroCache', ['$cacheFactory', function ($cacheFactory) {
    return $cacheFactory('$eCollabroCache');
}]);

ecollabroApp.directive('onRepeatFinished', function() {
    return function(scope, element, attrs) {
        if (scope.$last) setTimeout(function(){
            scope.$emit('onRepeatFinished', element, attrs);
        }, 1);
    };
})

ecollabroApp.config(['$compileProvider', function ($compileProvider) {
    $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|javascript):/);
}]);